<?php


namespace app\store\controller;


use app\common\service\Order as OrderService;
use app\store\model\Wxapp as WxappModel;
use app\common\library\wechat\WxPay;
class Supplier extends Controller
{
    public function index(){
        $list = db('supplier')->order('id desc')->paginate(15, false, [
            'query' => \request()->request()
        ]);
        return $this->fetch('index', compact('list'));
    }
    public function hklists(){
        $param = $this->request->param();
        $filter = [];
        if(isset($param['sid']) && $param['sid']){
            $filter['a.sid'] = $param['sid'];
        }
        if(isset($param['jkzt'])){
            $filter['a.status'] = $param['jkzt'];
        }
//        if($param['sid']){
//            $filter['a.sid'] = $param['sid'];
//        }
        $list = db('supplier_huokuan')->alias('a')
            ->field('a.*,b.name,b.tel')
            ->join('yoshop_supplier b','a.sid = b.id')
            ->where($filter)
            ->order('a.id desc')->paginate(15, false, [
                'query' => \request()->request()
            ]);
        $supplierList = db('supplier')->select();
        return $this->fetch('hklists', compact('list','supplierList'));
    }
    public function money($id){
        $udata['status'] = 1;
        $udata['pay_type'] = "线下付款";
        $result = db('supplier_huokuan')->where(['id'=>$id])->update($udata);
        if ($result) {
            return $this->renderSuccess('操作成功');
        }
        return $this->renderError('操作失败');
    }
    public function wechat_pay($id){
        $info = db('supplier_huokuan')->where(['id'=>$id])->find();
        $supplierInfo = db('supplier')->where(['id'=>$info['sid']])->find();
        // 生成付款订单号
        $orderNO = OrderService::createOrderNo();
        // 付款描述
        $desc = $info['title'];
        // 微信支付api：企业付款到零钱
        $wxConfig = WxappModel::getWxappCache();
        $WxPay = new WxPay($wxConfig);
        // 请求付款api
        if ($WxPay->transfers($orderNO, $info['open_id'], $info['money'], $desc,'FORCE_CHECK',$supplierInfo['name'])) {
            // 确认已打款
            $udata['status'] = 1;
            $udata['pay_type'] = "微信付款";
            $result = db('supplier_huokuan')->where(['id'=>$id])->update($udata);
            if ($result) {
                return $this->renderSuccess('操作成功');
            }
        }
        return $this->renderError('操作失败');
    }
    public function picLists($hkid){
        $list = db('supplier_pic')->where(['hkid'=>$hkid])->select();
        return $this->fetch('picLists', compact('list'));
    }
}